*******************
****DATA MERGER****
*******************

global user = "jon" // Add user-specific directories here

if "$user"=="jon" {
	global root "/Users/jonathanlweigel/Dropbox/Taxes/Stata/replication"
	global data "$root/data"
	global dofiles "$root/dofiles"
}

cap mkdir "$root/outputs"
cd "$root/outputs"

****************************
*****CLEAN ENDLINE DATA*****
****************************

use "$data/endline_clean.dta", clear

***************************************
*****MERGE IN TREATMENT ASSIGNMENT*****
***************************************

merge m:1 a7 using "$data/treatment_assignment.dta", keep(1 3) nogen

************************************
*****MERGE IN FLIER INFORMATION*****
************************************

merge 1:1 s3_code using "$data/flier_data.dta", keep(1 3) nogen

**************************************
*****MERGE IN PAYMENT INFORMATION*****
**************************************

merge 1:1 s3_code using "$data/payment_data_endline_sample.dta", keep(1 3) nogen

do "$dofiles/construct_payment_variables.do"

******************************************************
*****MERGE IN INDEPENDENT ASSESSMENT OF TAX RATES*****
******************************************************

merge 1:1 s3_code using "$data/taxrate_checks.dta", keep(1 3) nogen

******************************************************
*****MERGE IN ADDITIONAL TAX CAMPAIGN INFORMATION*****
******************************************************

merge 1:1 s3_code using "$data/campaign_info_indiv_level.dta" , keep(1 3) nogen

****************************************
*****MERGE IN COLLECTOR ASSIGNMENTS*****
****************************************

merge m:1 a7 using "$data/collector_assignments.dta", keep(1 3) nogen

save "$data/endline_clean_merged.dta", replace

***********************************
*****MERGE IN JIVE INSTRUMENTS*****
***********************************

global make_jive = 1

if $make_jive ==1 {
do "$dofiles/construct_jive_instruments"
}

use "$data/endline_clean_merged.dta", clear

merge m:1 a7 using "$data/jive_instruments.dta", keep(1 3) nogen 

global instruments = "paid_jive_u_avg registered_jive_u_avg"
foreach var of varlist $instruments{ 
replace `var' = . if program==0
sum `var'
replace `var' = (`var'-`r(min)')/(`r(max)'-`r(min)') 
winsor `var' , p(.05) gen(`var'_wins)
replace `var' = `var'_wins
drop `var'_wins
sum `var'
replace `var' = `r(min)' if program==0
}

save "$data/endline_clean_merged.dta", replace

*****************************************
*****MERGE IN PARTICIPATION OUTCOMES*****
*****************************************

merge m:1 s3_code using "$data/townhall_attendance.dta"
g townhall=1 if _m==3
*Set to missing for ppl who didn't have a chance to participate b/c they were selected after the conflict broke out or because they were sample2 participants
replace townhall=0 if townhalls_active==1 & sample2==0 & _m==1
drop _m
label var townhall "Townhall meeting attendance"

merge m:1 s3_code using "$data/submitted_evaluations.dta"
g evaluation = 1 if _m==3
replace evaluation=0 if sample2==0 & _m==1
drop _m
label var evaluation "Evaluation form submission"

*******************************************
*****MERGE IN ENDLINE SAMPLING WEIGHTS*****
*******************************************

merge 1:1 s3_code using "$data/endline_weights.dta", keep(1 3) nogen

***************************************
*****MERGE IN GEOGRAPHIC VARIABLES*****
***************************************

merge m:1 a7 using "$data/geographic_neighborhood_level.dta", keep(1 3) nogen

merge 1:1 s3_code using "$data/geographic_indiv_level.dta", keep(1 3) nogen

save "$data/endline_clean_merged.dta", replace

*****************************************************
*****CONSTRUCT VARIABLES FOR ANALYSIS - ENDLINE *****
*****************************************************

do "$dofiles/construct_endline_variables"

*****************************************************
*****CONSTRUCT VARIABLES FOR ANALYSIS - BASELINE*****
*****************************************************

use  "$data/baseline_clean.dta", clear

do "$dofiles/construct_baseline_variables.do"

merge m:1 a7 using "$data/treatment_assignment.dta", keep(1 3) nogen

g progXvisited_past = program * visited_past_hi
g progXbaseline_protest = program *past_protest_hi
label var visited_past_hi "Past visits (high)"
label var past_protest_hi "Past protest (high)"
label var progXvisited_past "Campaign X Past visits (high)"
label var progXbaseline_protest "Campaign X Past protest (high)"

merge 1:1 a7 using "$data/geographic_neighborhood_level.dta", keep(1 3) nogen

save "$data/neighborhood_covariates.dta", replace

use "$data/endline_clean_merged.dta", clear

merge m:1 a7 using "$data/neighborhood_covariates.dta", keep(1 3) nogen

save "$data/endline_clean_merged.dta", replace

****************************************************
*****CONSTRUCT VARIABLES FOR ANALYSIS - MIDLINE*****
****************************************************

use "$data/midline_clean.dta", clear

merge m:1 a7 using "$data/treatment_assignment.dta", keep(1 3) nogen

merge 1:1 s2_code using "$data/payment_full.dta", keep(1 3) nogen

merge m:1 a7 using "$data/geographic_neighborhood_level.dta", keep(1 3) nogen

save "$data/midline_clean_merged.dta", replace

do "$dofiles/construct_midline_variables" 

***************************************************
*****NEIGHBORHOOD LEVEL ANALYSIS OF COMPLIANCE*****
***************************************************

use "$data/payment_neighborhood_level.dta", clear

merge m:1 a7 using "$data/treatment_assignment.dta", keep(1 3) nogen

preserve
use "$data/midline_clean_merged.dta", clear
replace respondent=0 if respondent==.
collapse (count) respondent, by(a7)
rename respondent num_compounds
tempfile num_compounds_poly
save `num_compounds_poly'
restore

merge 1:1 a7 using `num_compounds_poly', keep(3) nogen

g compliance = paid / num_compounds
label var compliance "Property tax compliance (neighborhood level)"

g revenue_per_person = amount/ num_compounds
label var revenue_per_person "Property tax revenue per capita  (neighborhood level)"

save "$data/payment_neighborhood_level_merged.dta", replace

